#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<assert.h>
int main(){

    pid_t id = fork();
    assert(id != -1);
    if(id == 0){
        int cnt = 5;
        while(cnt){
            printf("我是子进程，我的pid是：%d，getppid:%d,cnt：%d\n",getpid(),getppid(),cnt--);
        sleep(1);
        }
        exit(23);
    }

    int status = 0;
    while(1){

        pid_t ret = waitpid(id+1,&status,WNOHANG);
        if(ret == 0){
            //子进程没退出
            printf("child is running...\n");
        }
        else if(ret > 0){
            //子进程退出了
            printf("wait success,exit code:%d,signal:%d\n",(status>>8)&0xFF,status&0x7F);
            break;
        }
        else{
            //waitpid调用失败
            printf("waitpid call failed\n");
        }
        sleep(1);
    }









    //if(ret>0){
    //    printf("ret:%d,status:%d,code:%d,signal:%d\n",ret,status,(status>>8)&0xFF,status&0x7F);
    //}
    return 0;
}
